﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Catering_DBOpertion;//Please add references
namespace Catering_Operation
{
	/// <summary>
	/// 数据访问类:ContractorInfo
	/// </summary>
	public partial class ContractorInfo
	{
		public ContractorInfo()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("id", "ContractorInfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from ContractorInfo");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add(Catering_Model.ContractorInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into ContractorInfo(");
            strSql.Append("[CID],[CName],[GroupID],[ClassID],[CardNo],[JobCard],[Effective],[Tel],[ConDesc],[OldClassID],[OldGroupID],[ChangeClassDate])");
			strSql.Append(" values (");
            strSql.Append("@CID,@CName,@GroupID,@ClassID,@CardNo,@JobCard,@Effective,@Tel,@ConDesc,@OldClassID,@OldGroupID,@ChangeClassDate)");
            SqlParameter[] parameters = {
					new SqlParameter("@CID", SqlDbType.Int,4),
					new SqlParameter("@CName", SqlDbType.VarChar,50),
					new SqlParameter("@GroupID", SqlDbType.VarChar,50),
                    new SqlParameter("@ClassID", SqlDbType.VarChar,50),
					new SqlParameter("@CardNo", SqlDbType.VarChar,50),
					new SqlParameter("@JobCard", SqlDbType.VarChar,50),
					new SqlParameter("@Effective", SqlDbType.VarChar,50),
					new SqlParameter("@Tel", SqlDbType.VarChar,50),
					new SqlParameter("@ConDesc", SqlDbType.Text,0),
					new SqlParameter("@OldClassID", SqlDbType.VarChar,50),
					new SqlParameter("@OldGroupID", SqlDbType.VarChar,50),
                    new SqlParameter("@ChangeClassDate", SqlDbType.DateTime,18)};
			parameters[0].Value = model.CID;
			parameters[1].Value = model.CName;
			parameters[2].Value = model.GroupID;
            parameters[3].Value = model.ClassID;
			parameters[4].Value = model.CardNo;
			parameters[5].Value = model.JobCard;
			parameters[6].Value = model.Effective;
			parameters[7].Value = model.Tel;
            parameters[8].Value = model.ConDesc;
            parameters[9].Value = model.OldClassID;
            parameters[10].Value = model.OldGroupID;
            parameters[11].Value = model.ChangeClassDate;
			DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Catering_Model.ContractorInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update ContractorInfo set ");
			strSql.Append("CID=@CID,");
			strSql.Append("CName=@CName,");
			strSql.Append("GroupID=@GroupID,");
            strSql.Append("ClassID=@ClassID,");
			strSql.Append("CardNo=@CardNo,");
			strSql.Append("JobCard=@JobCard,");
			strSql.Append("Effective=@Effective,");
			strSql.Append("Tel=@Tel,");
            strSql.Append("ConDesc=@ConDesc,");
            strSql.Append("OldClassID=@OldClassID,");
            strSql.Append("OldGroupID=@OldGroupID,");
            strSql.Append("ChangeClassDate=@ChangeClassDate");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@CID", SqlDbType.Int,4),
					new SqlParameter("@CName", SqlDbType.VarChar,50),
					new SqlParameter("@GroupID", SqlDbType.VarChar,50),
                    new SqlParameter("@ClassID", SqlDbType.VarChar,50),
					new SqlParameter("@CardNo", SqlDbType.VarChar,50),
					new SqlParameter("@JobCard", SqlDbType.VarChar,50),
					new SqlParameter("@Effective", SqlDbType.VarChar,50),
					new SqlParameter("@Tel", SqlDbType.VarChar,50),
					new SqlParameter("@ConDesc", SqlDbType.Text,0),
					new SqlParameter("@OldClassID", SqlDbType.VarChar,50),
					new SqlParameter("@OldGroupID", SqlDbType.VarChar,50),
                    new SqlParameter("@ChangeClassDate", SqlDbType.DateTime,18),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.CID;
			parameters[1].Value = model.CName;
			parameters[2].Value = model.GroupID;
            parameters[3].Value = model.ClassID;
			parameters[4].Value = model.CardNo;
			parameters[5].Value = model.JobCard;
			parameters[6].Value = model.Effective;
			parameters[7].Value = model.Tel;
            parameters[8].Value = model.ConDesc;
            parameters[9].Value = model.OldClassID;
            parameters[10].Value = model.OldGroupID;
            parameters[11].Value = model.ChangeClassDate;
			parameters[12].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ContractorInfo ");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ContractorInfo ");
            strSql.Append(" where " + idlist);
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Catering_Model.ContractorInfo GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 [ID],[CID],[CName],[GroupID],[ClassID],[CardNo],[JobCard],[Effective],[Tel],[ConDesc],[OldClassID],[OldGroupID],[ChangeClassDate] from ContractorInfo ");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = id;

			Catering_Model.ContractorInfo model=new Catering_Model.ContractorInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CID"]!=null && ds.Tables[0].Rows[0]["CID"].ToString()!="")
				{
					model.CID=int.Parse(ds.Tables[0].Rows[0]["CID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CName"]!=null && ds.Tables[0].Rows[0]["CName"].ToString()!="")
				{
					model.CName=ds.Tables[0].Rows[0]["CName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["GroupID"]!=null && ds.Tables[0].Rows[0]["GroupID"].ToString()!="")
				{
					model.GroupID=ds.Tables[0].Rows[0]["GroupID"].ToString();
				}
                if (ds.Tables[0].Rows[0]["ClassID"] != null && ds.Tables[0].Rows[0]["ClassID"].ToString() != "")
                {
                    model.ClassID = ds.Tables[0].Rows[0]["ClassID"].ToString();
                }
				if(ds.Tables[0].Rows[0]["CardNo"]!=null && ds.Tables[0].Rows[0]["CardNo"].ToString()!="")
				{
					model.CardNo=ds.Tables[0].Rows[0]["CardNo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["JobCard"]!=null && ds.Tables[0].Rows[0]["JobCard"].ToString()!="")
				{
					model.JobCard=ds.Tables[0].Rows[0]["JobCard"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Effective"]!=null && ds.Tables[0].Rows[0]["Effective"].ToString()!="")
				{
					model.Effective=ds.Tables[0].Rows[0]["Effective"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Tel"]!=null && ds.Tables[0].Rows[0]["Tel"].ToString()!="")
				{
					model.Tel=ds.Tables[0].Rows[0]["Tel"].ToString();
				}
                if (ds.Tables[0].Rows[0]["ConDesc"] != null && ds.Tables[0].Rows[0]["ConDesc"].ToString() != "")
				{
                    model.ConDesc = ds.Tables[0].Rows[0]["ConDesc"].ToString();
				}
                if (ds.Tables[0].Rows[0]["OldClassID"] != null && ds.Tables[0].Rows[0]["OldClassID"].ToString() != "")
				{
                    model.OldClassID = ds.Tables[0].Rows[0]["OldClassID"].ToString();
				}
                if (ds.Tables[0].Rows[0]["OldGroupID"] != null && ds.Tables[0].Rows[0]["OldGroupID"].ToString() != "")
				{
                    model.OldGroupID = ds.Tables[0].Rows[0]["OldGroupID"].ToString();
				}
                if (ds.Tables[0].Rows[0]["ChangeClassDate"] != null && ds.Tables[0].Rows[0]["ChangeClassDate"].ToString() != "")
                {
                    model.ChangeClassDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["ChangeClassDate"].ToString());
                }
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select [ID],[CID],[CName],[GroupID],[ClassID],[CardNo],[JobCard],[Effective],[Tel],[ConDesc],[OldClassID],[OldGroupID],[ChangeClassDate] ");
			strSql.Append(" FROM ContractorInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" [ID],[CID],[CName],[GroupID],[ClassID],[CardNo],[JobCard],[Effective],[Tel],[ConDesc],[OldClassID],[OldGroupID],[ChangeClassDate] ");
			strSql.Append(" FROM ContractorInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "ContractorInfo";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("GetPageData", parameters, "ContractorInfo");
		}

        /// <summary>
        /// 根据条件获取数据总条数
        /// </summary>
        public int GetCountBywhere(string where)
        {
            string strSql = string.Format("select count(*) from ContractorInfo where {0}", where);
            return Convert.ToInt32(DbHelperSQL.GetSingle(strSql));
        }

		#endregion  Method
	}
}

